﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DAL;
using System.Configuration;
public partial class admin_WebBaseInfo : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadBaseInfo();
        }
    }
    /// <summary>
    /// 加载基本信息数据
    /// </summary>
    private void LoadBaseInfo()
    {
        DataTable dt = BaseInfoDAL.Instance.GetBaseInfo();
        if (dt != null && dt.Rows.Count > 0)
        {
            txtTitle.Text = dt.Rows[0]["title"].ToString();
            txtKey.Text = dt.Rows[0]["keys"].ToString();
            txtBaseInfo.Text = dt.Rows[0]["base_info"].ToString();
            txtPhone.Text = dt.Rows[0]["phone"].ToString();
            txtBottomInfo.Text = dt.Rows[0]["bottom_info"].ToString();
            txtSMTP.Text = dt.Rows[0]["smtp"].ToString();
            txtEmail.Text = dt.Rows[0]["email"].ToString();
            txtEmailPassWord.Text = dt.Rows[0]["password"].ToString();
            txtWebMoneyName.Text = dt.Rows[0]["money_name"].ToString();
            txtMoney.Text = dt.Rows[0]["money"].ToString();
            if (dt.Rows[0]["logopath"].ToString() != "")
            {
                imgLogo.Visible = true;
                imgLogo.ImageUrl = @"..\uploadfile\" + dt.Rows[0]["logopath"].ToString();
            }
            if (dt.Rows[0]["is_close"].ToString() == "0")
            {
                radNo.Checked = true;
                radYes.Checked = false;
            }
            else
            {
                radYes.Checked = true;
                radNo.Checked = false;
            }
            txtAdminAccount.Text = dt.Rows[0]["admin_account"].ToString();
        }
    }

    /// <summary>
    /// 保存信息
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string[] strs = new string[13];
        strs[0] = txtTitle.Text;
        strs[1] = txtKey.Text;
        strs[2] = txtBaseInfo.Text;
        strs[3] = txtPhone.Text;
        strs[4] = SaveLogoFile(fileLogo);
        strs[5] = txtBottomInfo.Text;
        strs[6] = txtSMTP.Text;
        strs[7] = txtEmail.Text;
        strs[8] = txtEmailPassWord.Text;
        strs[9] = txtWebMoneyName.Text;
        strs[10] = txtMoney.Text;
        strs[11] = "1";
        if (radNo.Checked)
        {
            strs[11] = "0";
        }
        strs[12] = txtAdminAccount.Text.Trim();
        if (BaseInfoDAL.Instance.InsertBaseInfo(strs))
        {
            Alert("保存成功!");
        }
        else
        {
            Alert("保存失败，请重新保存!");
        }
    }

    /// <summary>
    /// 存储LOGO图片
    /// </summary>
    /// <param name="upload"></param>
    /// <returns></returns>
    private string SaveLogoFile(FileUpload upload)
    {
        string str = "";
        try
        {
            if (upload.HasFile)
            {
                string path = ConfigurationManager.AppSettings["uploadpath"];
                string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + upload.FileName.Substring(upload.FileName.Length - 5);
                upload.SaveAs(path + "\\" + fileName);
                str= fileName;
            }
            else
            {
                return "";
            }
        }
        catch (Exception ee)
        {
            return "";
        }
        return str;
    }
}